package com.marvels.financial.loan.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.loan.dto.EarlyPaymentCalculatDto;
import com.marvels.financial.loan.dto.ReplacePaymentApplyInfo;
import com.marvels.financial.loan.dto.ReplacePaymentConditionDto;
import com.marvels.financial.loan.service.ReplacePaymentApi;

@RestController
@RequestMapping("financial/loan/earlyPayment/service")
public class EarlyPaymentController {
	
	@Autowired
	private ReplacePaymentApi replacePaymentApi;

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
	
	/**
	 * 查询提前结清申请数据
	 * @param replacePaymentConditionDto
	 * @return
	 */
	@RequestMapping("earlyPaymentApplyList")
	@ResponseBody
	public MarvelsResultDTO earlyPaymentApplyList(@RequestBody ReplacePaymentConditionDto replacePaymentConditionDto) {
		try {
			return replacePaymentApi.earlyPaymentApplyInfo(replacePaymentConditionDto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询提前结清申请数据FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询提前结清申请数据异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "查询提前结清申请数据异常", null);
		}
	}

	
	/**
	 * 提前结清申请
	 * @param replacePaymentConditionDto
	 * @return
	 */
	@RequestMapping("earlyPaymentApply")
	@ResponseBody
	public MarvelsResultDTO earlyPaymentApply(@RequestBody ReplacePaymentConditionDto replacePaymentConditionDto) {
		try {
			replacePaymentApi.earlyPaymentApply(replacePaymentConditionDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					null);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>提前结清申请FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>提前结清申请申请处理异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "提前结清申请异常", null);
		}
	}
	
	
	
	/**
	 * 提前结清审核通过列表
	 * @param replacePaymentConditionDto
	 * @return
	 */
	@RequestMapping("earlyPaymentAdoptList")
	@ResponseBody
	public MarvelsResultDTO earlyPaymentAdoptList(@RequestBody ReplacePaymentConditionDto replacePaymentConditionDto) {
		try {
			Page<ReplacePaymentApplyInfo> list=replacePaymentApi.earlyPaymentAdoptList(replacePaymentConditionDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					list);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询提前结清审核通过列表FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询提前结清审核通过列表异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "查询提前结清审核通过列表异常", null);
		}
	}
	
	
	/**
	 * 提前结清申请拒绝列表
	 * @param replacePaymentConditionDto
	 * @return
	 */
	@RequestMapping("earlyPaymentRefuseList")
	@ResponseBody
	public MarvelsResultDTO earlyPaymentRefuseList(@RequestBody ReplacePaymentConditionDto replacePaymentConditionDto) {
		try {
			Page<ReplacePaymentApplyInfo> list=replacePaymentApi.earlyPaymentRefuseList(replacePaymentConditionDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					list);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询提前结清申请拒绝列表FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询提前结清申请拒绝列表异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "查询提前结清申请拒绝列表异常", null);
		}
	}
	
	
	/**
	 * 提前结清审核列表
	 * @param replacePaymentConditionDto
	 * @return
	 */
	@RequestMapping("earlyPaymentAuditList")
	@ResponseBody
	public MarvelsResultDTO earlyPaymentAuditList(@RequestBody ReplacePaymentConditionDto replacePaymentConditionDto) {
		try {
			List<ReplacePaymentApplyInfo> list=replacePaymentApi.earlyPaymentAuditList(replacePaymentConditionDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					list);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>提前结清审核列表查询FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>提前结清审核列表异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "提前结清审核列表异常", null);
		}
	}
	
	
	
	
	
	/**
	 * 提前结清审核通过
	 * @param replacePaymentConditionDto
	 * @return
	 */
	@RequestMapping("earlyPaymentAuditAdopt")
	@ResponseBody
	public MarvelsResultDTO earlyPaymentAuditAdopt(@RequestBody ReplacePaymentConditionDto replacePaymentConditionDto) {
		try {
			replacePaymentApi.earlyPaymentAuditAdopt(replacePaymentConditionDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					null);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>提前结清审核通过处理FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>提前结清审核通过异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "提前结清审核通过异常", null);
		}
	}
	
	
	
	/**
	 * 提前结清审核拒绝
	 * @param replacePaymentConditionDto
	 * @return
	 */
	@RequestMapping("earlyPaymentAuditRefuse")
	@ResponseBody
	public MarvelsResultDTO earlyPaymentAuditRefuse(@RequestBody ReplacePaymentConditionDto replacePaymentConditionDto) {
		try {
			replacePaymentApi.earlyPaymentAuditRefuse(replacePaymentConditionDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					null);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>提前结清审核拒绝处理FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>提前结清审核拒绝处理异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "提前结清审核拒绝处理异常", null);
		}
	}
	
	
	
	/**
	 * 提前结清试算
	 * @param replacePaymentConditionDto
	 * @return
	 */
	@RequestMapping("calculatEarlypayment")
	@ResponseBody
	public MarvelsResultDTO calculatEarlypayment(@RequestBody ReplacePaymentConditionDto replacePaymentConditionDto) {
		try {
			EarlyPaymentCalculatDto earlyPaymentCalculatDto=replacePaymentApi.calculatEarlypayment(replacePaymentConditionDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					earlyPaymentCalculatDto);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>提前结清试算处理FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>提前结清试算处理异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "提前结清试算处理异常", null);
		}
	}
}
